package controls
{
	import controls.events.ServiceEvent;
	import controls.sql.SqlServiceCenter;
	import controls.sql.SqlStatements;
	
	import data.ClassGroup;
	import data.ClassSection;
	import data.Course;
	import data.Degree;
	import data.Professor;
	import data.User;
	
	import flash.events.EventDispatcher;
	
	[Event(name="serviceConnected", source="controls.events.ServiceEvent")];
	
	public class DataRequestor extends EventDispatcher
	{
		// --------------- Singleton Implementation ----------------- //
		private static var _instance:DataRequestor;
		
		public static function get instance():DataRequestor
		{
			if(_instance == null)
				_instance = new DataRequestor();
			return _instance;
		}
		
		public function DataRequestor()
		{
			if(_instance != null)
				trace("Error: DataRequestor is a singleton.");
				
			initComponents();
		}
		// --------------- END Singleton Implementation ----------------- //
		
		// ---------------- Private Variables ---------------- //
		private var serviceCenter:SqlServiceCenter;
		private var sqlStatements:SqlStatements;
		public var connected:Boolean = false;
		// ---------------- END Private Variables ---------------- //
		
		// --------------- Initialization and Setup ----------------- //
		private function initComponents():void
		{
			serviceCenter = SqlServiceCenter.instance;
			sqlStatements = new SqlStatements();
			connected = serviceCenter.serviceReady;
			serviceCenter.addEventListener(ServiceEvent.SERVICE_CONNECTED, onConnection);
		}
		// --------------- END Initialization and Setup ----------------- //
		
		private function onConnection(evt:ServiceEvent):void
		{
			connected = evt.data;
			this.dispatchEvent(new ServiceEvent(ServiceEvent.SERVICE_CONNECTED, true));
		}
		
		// ---------------- External Request Functions ----------------- //
		public function checkUser(username:String, password:String, callback:Function):void
		{
			var sql:String = sqlStatements.checkUser(username, password);
			serviceCenter.makeDatabaseRequest(callback, sql, ['username', 'password'], [username, password]);
		}
		
		public function addUser(user:User, errorCallback:Function):void
		{
			var sql:String = sqlStatements.addUser(user);
			serviceCenter.makeDatabaseRequest(errorCallback, sql, null, null, errorCallback);
		}
		
		public function updateUser(user:User, errorCallback:Function):void
		{
			var sql:String = sqlStatements.updateUser(user);
			serviceCenter.makeDatabaseRequest(errorCallback, sql, null, null, errorCallback);
		}
		
		public function getCourseSections(course:Course, callback:Function):void
		{
			var sql:String = sqlStatements.getCourseSections(course);
			serviceCenter.makeDatabaseRequest(callback, sql);
		}
		
		public function getDegreeProgram(degree:Degree, callback:Function):void
		{
			var sql:String = sqlStatements.getDegree(degree);
			serviceCenter.makeDatabaseRequest(callback, sql, ['degree'], [degree]); 
		}
		
		public function getDegreeProgramById(degreeId:int, callback:Function):void
		{
			var sql:String = sqlStatements.getDegreeById(degreeId);
			serviceCenter.makeDatabaseRequest(callback, sql, ['degreeId'], [degreeId]); 
		}
		
		public function getDegreeClass(courseInfo:Object, degree:Degree, callback:Function):void
		{
			var sql:String = sqlStatements.getClass(courseInfo.courseID);
			serviceCenter.makeDatabaseRequest(callback, sql, ['degree'], [degree]);
		}
		
		public function getClassGroup(group:ClassGroup, callback:Function):void
		{
			var sql:String = sqlStatements.getClassGroup(group.id);
			serviceCenter.makeDatabaseRequest(callback, sql);
		}
		
		public function getSectionGPA(section:ClassSection, callback:Function):void
		{
			var sql:String = sqlStatements.getSectionGPA(section);
			serviceCenter.makeDatabaseRequest(callback, sql);
		}
		
		public function getProfessorGPA(professor:Professor, callback:Function):void
		{
			var sql:String = sqlStatements.getProfessorGPA(professor);
			serviceCenter.makeDatabaseRequest(callback, sql, null, null, callback);
		}
		
		public function getProfessorCourseGPA(professor:Professor, title:String, callback:Function):void
		{
			var sql:String = sqlStatements.getProfessorCourseGPA(professor, title);
			serviceCenter.makeDatabaseRequest(callback, sql);
		}
		
		public function getCourseGPA(course:Course, callback:Function):void
		{
			var sql:String = sqlStatements.getCourseGPA(course);
			serviceCenter.makeDatabaseRequest(callback, sql);
		}
		
		public function getMajors(callback:Function):void
		{
			var sql:String = sqlStatements.getMajors();
			serviceCenter.makeDatabaseRequest(callback, sql);
		}
		
		public function getMinors(callback:Function):void
		{
			var sql:String = sqlStatements.getMinors();
			serviceCenter.makeDatabaseRequest(callback, sql);
		}
		
		public function getCertificates(callback:Function):void
		{
			var sql:String = sqlStatements.getCertificates();
			serviceCenter.makeDatabaseRequest(callback, sql);
		}
		// ---------------- END External Request Functions ----------------- //
	}
}